﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ExportPhysics.DTO;
using System.Xml;
using System.Globalization;

namespace ExportPhysics.DAO
{
    public class XMLHelper
    {
        public static int GetAttributeAsHex(XmlNode node, string attName)
        {
            int outInt = 0;
            int.TryParse(((XmlElement)node).GetAttribute(attName)
                    , NumberStyles.AllowHexSpecifier, CultureInfo.InvariantCulture.NumberFormat, out outInt);
            return outInt;
        }
        public static int GetAttributeAsInt(XmlNode node, string attName)
        {
            int outInt = 0;
            int.TryParse(((XmlElement)node).GetAttribute(attName)
                    , out outInt);
            return outInt;
        }

        internal static float GetAttributeAsFloat(XmlNode node, string attName)
        {
            float outFloat = 0;
            float.TryParse(((XmlElement)node).GetAttribute(attName), out outFloat);
            return outFloat;
        }
    }
    public class b2BodyDAO
    {
        public static bool Save(string filename, b2Body b2Body)
        {
           CFileWriter writer = CFileWriter.Open(filename);
            if (writer == null)
                return false;

            writer.WriteInt(b2Body.m_shapeList.Count);
            foreach (b2PolygonShape polygonShape in b2Body.m_shapeList)
            {
                writer.WriteInt(polygonShape.vertexCount);
                for (int i = 0; i < polygonShape.vertexCount; i++)
                {
                    writer.WriteFloat(polygonShape.vertices[i].Y);
                    writer.WriteFloat(polygonShape.vertices[i].X);                  
                }
            }

            writer.Release();
            return true;
        }
        public static b2Body Load(string filename)
        {
            b2Body b2Body = new b2Body();

            XmlDocument Doc = new XmlDocument();
            Doc.Load(filename);
            XmlElement root = Doc.DocumentElement;

            XmlNodeList polygonNodes = root.SelectNodes(@"//polygon");

            float minX, minY, maxX, maxY;
            minX = minY = maxX = maxY = -1;

            foreach (XmlNode polygonNode in polygonNodes)
            {
                b2PolygonShape polygonShape = new b2PolygonShape();

                XmlNodeList vertexNodes = polygonNode.SelectNodes("vertex");
                foreach (XmlNode vertexNode in vertexNodes)
                {
                    polygonShape.vertices[polygonShape.vertexCount] = new vector2df();
                    polygonShape.vertices[polygonShape.vertexCount].X = XMLHelper.GetAttributeAsFloat(vertexNode, "x");
                    polygonShape.vertices[polygonShape.vertexCount].Y = XMLHelper.GetAttributeAsFloat(vertexNode, "y");


                    if (minX == -1 || minX > polygonShape.vertices[polygonShape.vertexCount].X)
                        minX = polygonShape.vertices[polygonShape.vertexCount].X;
                    if (maxX == -1 || maxX < polygonShape.vertices[polygonShape.vertexCount].X)
                        maxX = polygonShape.vertices[polygonShape.vertexCount].X;
                    if (minY == -1 || minY > polygonShape.vertices[polygonShape.vertexCount].Y)
                        minY = polygonShape.vertices[polygonShape.vertexCount].Y;
                    if (maxY == -1 || maxY < polygonShape.vertices[polygonShape.vertexCount].Y)
                        maxY = polygonShape.vertices[polygonShape.vertexCount].Y;
                    polygonShape.vertexCount++;
                }

                b2Body.m_shapeList.Add(polygonShape);
            }

            float w = maxX - minX;
            float h = maxY - minY;
            foreach (b2PolygonShape polygonShape in b2Body.m_shapeList)
            {
                for (int i = 0; i < polygonShape.vertexCount; i++)
                {
                    polygonShape.vertices[i].X -= minX;
                    polygonShape.vertices[i].Y -= minY;

                    polygonShape.vertices[i].X /= h;
                    polygonShape.vertices[i].Y /= h;
                }
            }

            return b2Body;
        }
    }
}
